

import java.io.*;

public class Test0210_CharsetConvention {
    public static void main(String[] args) {
        // 源文件编码格式
        String srcEncoding = "GBK";
        // 要转换的编码格式
        String distEncoding = "UTF-8";
        // 源文件路径
        String srcFilePath = "input.txt"; // 替换为你的源文件路径
        // 目标文件路径
        String distFilePath = "output.txt"; // 替换为你的目标文件路径

        try (
            // 创建输入流，指定源文件编码为GBK
            InputStreamReader isr = new InputStreamReader(new FileInputStream(srcFilePath), srcEncoding);
            // 创建输出流，指定目标文件编码为UTF-8
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(distFilePath), distEncoding);
            BufferedReader br = new BufferedReader(isr);
            BufferedWriter bw = new BufferedWriter(osw)
        ) {
            String line;
            // 逐行读取源文件并写入目标文件
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
            System.out.println("文件编码转换完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}